package com.sr.vo.req;

import com.sr.pojo.SysUser;
import com.sr.util.ToolUtil;
import com.sr.vo.BaseForm;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.util.StringUtils;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.PositiveOrZero;
import javax.validation.constraints.Size;
import java.util.Date;


/**
 * @author xiaochi
 * @date 2021/12/15 21:04
 * @desc UserAddReqVo
 */
@ApiModel("用户信息")
@Data
@EqualsAndHashCode(callSuper = true)
public class UserReqVo extends BaseForm<SysUser> {

    @ApiModelProperty("用户id，更新时必须")
    @NotBlank(message = "用户id不能为空",groups = Update.class)
    @Size(min = 19,max = 19,message = "id不正确",groups = Update.class)
    private String id;

    @ApiModelProperty("用户账号")
    @NotBlank(message = "账号不能为空",groups = Create.class)
    private String username;

    @ApiModelProperty("用户密码")
    @NotBlank(message = "密码不能为空",groups = Create.class)
    @Size(min = 8,max = 16,message = "密码长度为{min} ~ {max}位",groups = {Create.class,Update.class})
    private String password;

    @ApiModelProperty("手机号")
    @Pattern(regexp = "1[3|4|5|7|8][0-9]\\d{8}",message = "手机不正确",groups = {Create.class,Update.class})
    private String phone;// 手机号

    @ApiModelProperty("邮件地址")
    @Email(message = "非法邮件地址",groups = {Create.class,Update.class})
    private String email;// 邮箱

    @ApiModelProperty("昵称")
    private String nickName;// 昵称

    @ApiModelProperty("真实姓名")
    private String realName;// 真实姓名

    @ApiModelProperty("性别")
    @PositiveOrZero(message = "性别非法")
    private Integer sex;// 性别

    @ApiModelProperty("创建来源(1.web 2.android 3.ios )")
    @PositiveOrZero(message = "来源非法")
    private Long createWhere;// 创建来源(1.web 2.android 3.ios )

    @ApiModelProperty("账户状态(1.正常 2.锁定 )")
    @PositiveOrZero(message = "账户状态非法")
    private Integer status;// 账户状态(1.正常 2.锁定 )

    /**
     * 获取实例
     * @return 返回实体类
     */
    @Override
    public SysUser buildEntity() {
        SysUser sysUser = ToolUtil.copy(this, SysUser.class);
        Date date = new Date();
        if (StringUtils.isEmpty(sysUser.getId())){
            sysUser.setId(ToolUtil.nextId());
            sysUser.setCreateTime(date);
            sysUser.setUpdateTime(date);
        }else{
            sysUser.setUpdateTime(date);
        }
        return sysUser;
    }
}
